<?php

namespace App\Orchid\Layouts\Page;

use App\Models\Page;
use Orchid\Screen\TD;
use Orchid\Screen\Actions\Link;
use Orchid\Screen\Layouts\Table;

class PageListLayout extends Table
{
    /**
     * Data source.
     *
     * The name of the key to fetch it from the query.
     * The results of which will be elements of the table.
     *
     * @var string
     */
    protected $target = 'pages';

    /**
     * Get the table cells to be displayed.
     *
     * @return TD[]
     */
    protected function columns(): iterable
    {
        return [
            TD::make('title', '页面标题')
                ->sort()
                ->filter(TD::FILTER_TEXT)
                ->render(function (Page $page) {
                    return Link::make($page->title)
                        ->route('platform.pages.edit', $page);
                }),
                
            TD::make('slug', 'Slug')
                ->sort(),
                
            TD::make('user.name', '作者'),
            
            TD::make('status', '状态')
                ->render(function (Page $page) {
                    return match($page->status ?? 'draft') {
                        'published' => '<span class="text-success">已发布</span>',
                        'draft' => '<span class="text-warning">草稿</span>',
                        default => $page->status,
                    };
                }),
                
            TD::make('template', '模板')
                ->render(function (Page $page) {
                    return $page->template ?: 'default';
                }),
                
            TD::make('created_at', '创建时间')
                ->sort()
                ->render(function (Page $page) {
                    return $page->created_at->format('Y-m-d H:i');
                }),
                
            TD::make('操作')
                ->align(TD::ALIGN_CENTER)
                ->width('150px')
                ->render(function (Page $page) {
                    return Link::make('编辑')
                        ->icon('pencil')
                        ->route('platform.pages.edit', $page);
                }),
        ];
    }
}